
//console.log(domain.staticzt);
var Load="";
$(function(){  
    if(domain.staticzt=="1"&&sessionStorage.getItem("Load")!="123"){
   
   var json = [
           {
                   "id": "1",
                   "text": "实时监控",
                   "icon": "icon-search",
                   "url": "",
                   "menus": [
                       {
                           "id": "1",
                           "text": "设备监控",
                           "icon": "icon-search",
                           "url": "#/page/Monitoring/list"
                       }
                        
                   ]
               },
               {
                   "id": "2",
                   "text": "基础管理",
                   "icon": "icon-file",
                   "url": "",
                   "menus": [
                       {
                           "id": "1",
                           "text": "用户管理",
                           "icon": "icon-file",
                           "url": "#/page/user/list"
                       },
                         {
                             "id": "2",
                             "text": "单位管理",
                             "icon": "icon-file",
                             "url": "#/page/Company/list"
                         },
                       {
                           "id": "3",
                           "text": "角色管理",
                           "icon": "icon-file",
                           "url": "http://www.jd.com"
                       },
                       {
                           "id": "4",
                           "text": "电子围栏",
                           "icon": "icon-file",
                           "url": "http://www.jd.com"
                       }
                   ]
               },
               {
                   "id": "3",
                   "text": "系统管理",
                   "icon": "icon-cog",
                   "url": "",
                   "menus": [
                        {
                            "id": "1",
                            "text": "模块管理",
                            "icon": "icon-cog",
                            "url": "/Yunjuanscxcx/web/page/alumnus/zxlist.html?r=0.1"
                        }
                   ]
               },
                 {
                     "id": "4",
                     "text": "设备管理",
                     "icon": "icon-hdd",
                     "url": "",
                     "menus": [
                       
                        {
                            "id": "1",
                            "text": "设备分组",
                            "icon": "icon-hdd",
                            "url": "/Yunjuanscxcx/web/page/Industry/list.html?r=0.1"
                        },
                         {
                             "id": "2",
                             "text": "设备列表",
                             "icon": "icon-hdd",
                             "url": "/Yunjuanscxcx/web/page/department/list.html?r=0.1"
                         },
                            {
                             "id": "3",
                             "text": "设备类型",
                             "icon": "icon-hdd",
                             "url": "/Yunjuanscxcx/web/page/department/list.html?r=0.1"
                         },
                           {
                             "id": "4",
                             "text": "设备分布",
                             "icon": "icon-hdd",
                             "url": "/Yunjuanscxcx/web/page/department/list.html?r=0.1"
                         },
                             {
                             "id": "5",
                             "text": "设备路线轨迹",
                             "icon": "icon-hdd",
                             "url": "/Yunjuanscxcx/web/page/department/list.html?r=0.1"
                         }
                     ]
                 },
   
   
               {
                   "id": "5",
                   "text": "节目管理",
                   "icon": "icon-facetime-video",
                   "url": "",
                   "menus": [
                       {
                           "id": "1",
                           "text": "添加节目",
                           "icon": "icon-facetime-video",
                           "url": "/Yunjuanscxcx/web/page/usermanagement/list.html?r=0.1"
                       },
                             {
                           "id": "2",
                           "text": "节目类型",
                           "icon": "icon-facetime-video",
                           "url": "/Yunjuanscxcx/web/page/usermanagement/list.html?r=0.1"
                       },
                             {
                           "id": "3",
                           "text": "节目列表",
                           "icon": "icon-facetime-video",
                           "url": "/Yunjuanscxcx/web/page/usermanagement/list.html?r=0.1"
                       },
                   ]
               }
               ];
//console.log(domain.staticzt);

            $("#menu li").click(function(){
                $("#sgd").addClass("nui-scroll");
 if($(this).index()!="0"){
   $("#menu li").eq(0).children("div").css("display","none");
   }
   });
  $('#tabs').addtabs({ monitor: '.topbar' });
}


   });
   //单击菜单事件
   window.zTreeBeforeClick=function(treeId, treeNode, clickFlag) {


    
    var title=$("#tabSuperior").find(".active").find("a").text();

   var id=$("#tabSuperior").find(".active").find("a").attr("href");
  
   id=id.split('_');
   
   id=id[1];

 //alert($("#tabSuperior").find(".active").find("a").attr("href"));
  var url=$("#iftab_"+id).attr("src");
 
  $("#tabSuperior li").each(function(i){
     if(parseInt(i)>1){ 
        $(this).remove();
     }
    });
     $(".tab-content div").each(function(i){
        if(parseInt(i)>0){ 
           $(this).remove();
        }
  });
  
  var childNodes = $.fn.zTree.getZTreeObj("treeDemo").transformToArray(treeNode);
   try{

    sessionStorage.setItem("typetow",childNodes[1].type);

   }catch(ex){
    sessionStorage.setItem("typetow",childNodes[0].type);
   
   }
       
  //console.log();

  $("#Firstactive").click();
  $("#Firstactive").addClass("active");
    sessionStorage.setItem("coid",treeNode.id);
    sessionStorage.setItem("type",treeNode.type);
    sessionStorage.setItem("high",treeNode.high);
    sessionStorage.setItem("width",treeNode.width);
    sessionStorage.setItem("isgroup",treeNode.isgroup);
    sessionStorage.setItem("isParent",typeof(treeNode.isParent)=="undefined"?"false":treeNode.isParent);
    

    var str ='' ;
    str=getChildNodes(treeNode,str);
   
   
   
  

    try{

    }catch(ex){

    }
    $(".navbar-brand").find("span").text(" 广告信息发布平台-"+treeNode.name);
    //return (treeNode.id !== 1);

    if(title!="首页"&&typeof(url)!="undefined"){
        addTabs({id:id,title:title,close: true,url: url});
       }
      console.log("iframescr");
       window.frames["iframescr"].window.listsx()
      //document.getElementById('iframescr').contentWindow.location.reload();
     // document.getElementById('iframescr').cxlist();
};
function getChildNodes(treeNode,result){
    if (treeNode.isParent) {
      var childrenNodes = treeNode.children;
      if (childrenNodes) {
          for (var i = 0; i < childrenNodes.length; i++) {
              result += ',' + childrenNodes[i].id;
              result = getChildNodes(childrenNodes[i], result);
          }
      }
  }
  return result;
}


window.imgerror=function(img){
    img.src="static/img/no.png";
    img.onerror=null;   //控制不要一直跳动
    }
    //加载菜单
    window.MenuTree=function(i){

        //刷新菜单，最好把加载的东西放在他的前面执行，要不然可能被刷掉失效
        sessionStorage.setItem("Load","123");
        var deviceHeight = document.documentElement.clientHeight;
       $("#sgd").height((Number(deviceHeight)-50));
       $("#usname").text("欢迎光临,"+sessionStorage.getItem("username"));
       $("#loginname").val(sessionStorage.getItem("username"));
       $(".sidebar-collapse").click(function(){
         // console.log(123);
       $("#sgd").removeClass("nui-scroll");
       $("#sidebar").attr("style",""); 
       $("#menu li").eq(0).children("div").css("display","none");
           //$("#menu li").eq(0).children("div").css("display","none");
       });
    
       
       
         $("#Logout").click(function(){
           sessionStorage.removeItem("sid");
           location.href="/test/dist/index.html#/Login";
         });
             //密码
          $("#password").blur(function(){  
              if($("#password").val().length==0){
         $("#myModal span").eq(0).html("请输入原始密码");
        // $("#password").focus();
               return;
              }else{
              $("#myModal span").eq(0).text("");
              }
           //if($("#password").val().length>0 && $("#password").length<6){
            //   $("span").eq(0).text("长度只能在6-20个字符之间");
              //   return;
         //  }
        
           domain.ajaxUrl({
           method: 'POST',
           url: domain.testUrl+"/Threeinjection/index/modification/queryuserpwdByuserName",
           data: {userName:$("#loginname").val(),userPwd:$("#password").val(),"sessionid":sessionStorage.getItem("sid")},
           datatype:"JSON",
           success: function (response) {
           //   var json=JSON.parse(response);
         
              if(response=="\"no\""){
                 
                  $("#myModal span").eq(0).html("原始密码输入不正确");   //$("#password").focus();
                 return;
              }else{
       $("#password1").blur();$("#password2").blur();
                  $("#myModal span").eq(0).text("");
              }
        $("#password1").blur(function(){  
            // $("#myModal span").text("");
              if($("#password1").val().length==0){ //$("#password1").focus();
        $("#myModal span").eq(1).text("请输入新密码");
               return;
              }
       if($("#password1").val().length>0 && $("#password1").val().length<6){ //$("#password1").focus();
             $("#myModal span").eq(1).text("长度只能在6-20个字符之间");
             return;
       }
         $("#myModal span").eq(1).text("");
       });
        $("#password2").blur(function(){
          if($("#password2").val()!=$("#password1").val()){
               //$("#password2").focus();
       $("#myModal span").eq(2).text("输入新密码和确认密码不一致");
               return;
              }
        $("#myModal span").eq(2).text("");
       });
       
            
           }
       });
       
       });
       
          
       
         $("#add").click(function(){
            if($("#password").val().length==0){
         $("#myModal span").eq(0).html("请输入原始密码");
         $("#password").focus();
               return;
              }else{
              $("#myModal span").eq(0).text("");
              }
           //if($("#password").val().length>0 && $("#password").length<6){
            //   $("span").eq(0).text("长度只能在6-20个字符之间");
              //   return;
         //  }
           domain.ajaxUrl({
           method: 'POST',
           url: domain.testUrl+"/Threeinjection/index/modification/queryuserpwdByuserName",
           data: {userName:$("#loginname").val(),userPwd:$("#password").val(),"sessionid":sessionStorage.getItem("sid")},
           datatype:"JSON",
           success: function (response) {
           //   var json=JSON.parse(response);
              if(response=="\"no\""){
                  $("#myModal span").eq(0).html("原始密码输入不正确");   
                  $("#password").focus();
                 return;
              }else{
                  $("#myModal span").eq(0).text("");
              }
            // $("#myModal span").text("");
              if($("#password1").val().length==0){ 
                  $("#password1").focus();
        $("#myModal span").eq(1).text("请输入新密码");
               return;
              }
       if($("#password1").val().length>0 && $("#password1").val().length<6){ 
           $("#password1").focus();
             $("#myModal span").eq(1).text("长度只能在6-20个字符之间");
             return;
       }
         $("#myModal span").eq(1).text("");
          if($("#password2").val()!=$("#password1").val()){
               $("#password2").focus();
       $("#myModal span").eq(2).text("输入新密码和确认密码不一致");
               return;
              }
        $("#myModal span").eq(2).text("");
         //$('#myModal').modal('show');
           // console.log("123");
           domain.ajaxUrl({
           method: 'POST',
           url: domain.testUrl+"/Threeinjection/index/modification/updateUserPwdByUserName",
           data: {userName:$("#loginname").val(),userPwd:$("#password2").val(),"sessionid":sessionStorage.getItem("sid")},
          // datatype:"JSON",
           success: function (response) {
            //    console.log(response);
               if(response=="ok"){
                      swal("success!", "修改成功", "success");
                       // $("#password1").val("");
                    //  $("#password").val("");  $("#password2").val("");
                      location.href="/test/dist/index.html#/Login"; 
                      $(".modal-header button").click();
               }
           }
           });
           }
           });
         });



    domain.ajaxUrl({
        method: 'POST',
        url: domain.testUrl+"/Threeinjection/index/queryHomePageDataByUserId",
        data: {"sessionid":sessionStorage.getItem("sid"),"coid":sessionStorage.getItem("companyid")},
        datatype:"JSON",
        success: function (response) {  
            // console.log(response);
            domain.staticzt=false;
   console.log("首页");
            var obj = eval("("+response+")");
           // console.log( obj.module);
          
      //  console.log("123:"+JSON.parse(response.module[0]));
       if(i==0){
           $('#menu').sidebarMenu({//创建left菜单
               //data: "./scripts/json/Menu.json?r=0.1",
                 data:JSON.parse(obj.module)
                 //等于0刷新菜单
           });
        }else{

            select=true;
            //清除，默认选中卡组
        }
           var setting = {   
               callback: {
            beforeClick: zTreeBeforeClick
        }
          //  async: {
            //  enable: true,
             // url: domain.testUrl+"/Threeinjection/index/queryHomePageDataByUserId?sessionid="+sessionStorage.getItem("sid"),//异步加载时的请求地址
             // autoParam: ["id"],//提交参数
             // type: 'get',
             // dataType: 'json'
      //  },
        };
        // console.log(obj.company);
     var roletree=$.fn.zTree.init($("#treeDemo"), setting,JSON.parse(obj.company));
      var node = roletree.getNodes()[0];
      sessionStorage.setItem("coid",node.id)
       var str ='' ;
       str=getChildNodes(node,str);
       sessionStorage.setItem("logo",node.logo);  
      // $("#logo").attr("src",domain.ipimg+"/recordfile/lgsvfullcolor/1541845592508.gif"); 
    //    console.log(node.logo);
       if(node.logo!=""){
        // console.log("ok");
        $("#logo").attr("src",domain.ipimg+"/"+node.logo);
       }else{

        $("#logo").attr("src","../assets/no.png");
       }
       sessionStorage.setItem("type",node.type);
       sessionStorage.setItem("high",node.high);
       sessionStorage.setItem("width",node.width);
       sessionStorage.setItem("isgroup",node.isgroup);
       sessionStorage.setItem("isParent",typeof(node.isParent)=="undefined"?"false":node.isParent);
      // getChildNodestow(node);
       var childNodes = roletree.transformToArray(node);
       try{
         
        sessionStorage.setItem("typetow",childNodes[1].type);
       }catch(ex){

        sessionStorage.setItem("typetow",childNodes[0].type);
       }
      
     //  console.log(childNodes[0].type);
        if(node.isgroup=="1"){

            $("#company").hide();

            $(".submenu").find(".menu-text").each(function(){

                console.log("is:"+$(this).text());
                 if($(this).text()=="单位管理"){

                    
                    $(this).parent().hide();
                 }

            });
                
            
        }
    //默认选择卡组
    var nodes = roletree.getNodes();
    if(nodes[0].isgroup=="1"){
        roletree.selectNode(nodes[0]);
    }
      roletree.selectNode(node);
      $(".navbar-brand").find("span").text(" 广告信息发布平台-"+node.name);
      if(nodes[0].children!=undefined){//当前节点包含子节点
          getNodesId(nodes[0].children,roletree);//获取当前节点的子节点的id
      }

       //alert(node.id+str);
     
      // node.name
       //获取第一个节点
      // setting.callback.onClick(null, roletree.setting.treeId, node);
      // $("#treeDemo li").eq(0).click();
        }
    });
}

//默认选择卡组
var select=true;
            function getNodesId(nodes,roletree){
                for(var j=0;j<nodes.length;j++){
                if(select){
                // console.log(nodes[j].name+","+nodes[j].isgroup);
                if(nodes[j].isgroup=="1"){
                    roletree.selectNode(nodes[j]);
                    sessionStorage.setItem("coid",nodes[j].id);
                   // sessionStorage.setItem("typetow",nodes[j].type);
                    sessionStorage.setItem("type",nodes[j].type);
                    sessionStorage.setItem("high",nodes[j].high);
                    sessionStorage.setItem("width",nodes[j].width);
                    sessionStorage.setItem("isgroup",nodes[j].isgroup);
                    sessionStorage.setItem("isParent",typeof(nodes[j].isParent)=="undefined"?"false":nodes[j].isParent);
                    var childNodes = roletree.transformToArray(nodes[j]);

                    $(".navbar-brand").find("span").text(" 广告信息发布平台-"+nodes[j].name);

                    try{
                     sessionStorage.setItem("typetow",childNodes[1].type);
                    }catch(ex){
                     sessionStorage.setItem("typetow",childNodes[0].type);
                    }
                    select=false;
                }
            }
                    if(nodes[j].children!=undefined&&select){//当前节点包含子节点
                        getNodesId(nodes[j].children,roletree);//递归，获取当前节点的子节点的id

                    }
                }
            }
     